package mtt.us.client.views.fachbereich.matrikel;

import mtt.us.client.events.GetModelDataEvent;
import mtt.us.client.views.fachbereich.matrikel.editor.MatrikelEditorController;
import mtt.us.client.views.fachbereich.matrikel.editor.MatrikelEditorModel;
import mtt.us.client.views.fachbereich.matrikel.editor.MatrikelEditorView;
import mtt.us.client.views.fachbereich.matrikel.editor.events.ShowMatrikelEditorViewEvent;
import mtt.us.client.views.fachbereich.matrikel.events.AddMatrikelEvent;
import mtt.us.client.views.fachbereich.matrikel.events.EditMatrikelEvent;
import mtt.us.client.views.fachbereich.matrikel.events.ShowMatrikelViewEvent;
import mtt.us.hmvcFramework.client.hmvc.events.AddViewEvent;
import hmvc4gwt.client.controller.Controller;
import hmvc4gwt.client.event.HmvcEvent;
import hmvc4gwt.client.event.HmvcEventListener;


public class MatrikelController extends Controller implements HmvcEventListener {

	public void doInit() {
		
		register(ShowMatrikelViewEvent.class, this);
		register(AddMatrikelEvent.class, this);
		register(EditMatrikelEvent.class, this);
		
	}

	public void handleEvent(HmvcEvent event) {
		
		if (event instanceof ShowMatrikelViewEvent) {
			fireLocalEvent(new GetModelDataEvent());
			
			fireEvent(new AddViewEvent(getView()), true, false, false);
		} else if (event instanceof AddMatrikelEvent) {
			
			addChildTriad(new MatrikelEditorModel(), new MatrikelEditorView(), new MatrikelEditorController());
			
			fireEvent(new ShowMatrikelEditorViewEvent(), false, false, true);
			
		} else if (event instanceof EditMatrikelEvent) {
			
			EditMatrikelEvent editMatrikelEvent = (EditMatrikelEvent) event;
			
			addChildTriad(new MatrikelEditorModel(editMatrikelEvent.getMatrikel()), new MatrikelEditorView(), new MatrikelEditorController());
			
			fireEvent(new ShowMatrikelEditorViewEvent(), false, false, true);
		} else if (event instanceof AddMatrikelEvent) {
			
			addChildTriad(new MatrikelEditorModel(), new MatrikelEditorView(), new MatrikelEditorController());
			
			fireEvent(new ShowMatrikelEditorViewEvent(), false, false, true);
			
		}
		
	}
	
}
